package com.ztom.v2;

/**
 * @author ZhangTao
 */
public class Code91UniquePath {

    public int uniquePaths(int m, int n) {
//        return process(m, n, 0, 0);

        int[][] dp = new int[m][n];
        dp[m - 1][n - 1] = 1;
        // 右边界
        for (int i = 0; i < m; i++) {
            dp[i][n - 1] = 1;
        }
        // 左边界
        for (int i = 0; i < n; i++) {
            dp[m - 1][i] = 1;
        }
        for (int i = m - 2; i >= 0; i--) {
            for (int j = n - 2; j >= 0; j--) {
                dp[i][j] = dp[i + 1][j] + dp[i][j + 1];
            }
        }
        return dp[0][0];
    }

    public int process(int m, int n, int x, int y) {
        if (x == m - 1 && y == n - 1) {
            // 终点
            return 1;
        }
        // 下边界只能向右
        if (x == m - 1) {
            return process(m, n, x, y + 1);
        }
        // 右边界只能向下
        if (y == n - 1) {
            return process(m, n, x + 1, y);
        }
        // 向右或向下
        return process(m, n, x + 1, y) + process(m, n, x, y + 1);
    }
}
